<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
//个人中心控制器
class CenterController extends CommonController{
	private $model;
	public function __construct(){
		parent::__construct();
		$this->model = new \Home\Model\Address;
	}
	//个人中心首页
	public function index(){
		//分页处理***************
		//统计总数 SELECT count(*) FROM .....
		$total = Db::table('record')->join('goods','gid','=','goods_gid')->where("user_uid={$_SESSION['userid']}")->count();
		//执行分页
		$page = Page::row(2)->make($total);
		View::with('page',$page);
		
		
		//夺宝记录
		$recordData = Db::table('record')->join('goods','gid','=','goods_gid')->limit(Page::limit())->where("user_uid={$_SESSION['userid']}")->get();
		//sp($recordData);
		View::with('recordData',$recordData);
		
		//登录用户的账户余额
		$userData = Db::table('user')->where("userid={$_SESSION['userid']}")->field('sum')->get();
		View::with('userData',$userData);
	    View::make();
	}
	
	//夺宝记录
	public function record(){
		//分页处理***************
		//统计总数 SELECT count(*) FROM .....
		$total = Db::table('record')->join('goods','gid','=','goods_gid')->where("user_uid={$_SESSION['userid']}")->count();
		//执行分页
		$page = Page::row(3)->make($total);
		View::with('page',$page);
		
		
		//夺宝记录
		$recordData = Db::table('record')->join('goods','gid','=','goods_gid')->limit(Page::limit())->where("user_uid={$_SESSION['userid']}")->get();
		//sp($recordData);
		View::with('recordData',$recordData);
		
		
	    View::make();
	}
	
	//收货地址
	public function address(){
		
		//显示的收货地址必须是当前登录用户的地址所以要关联！！！！！！！！！！！！
		$addressData = $this->model->where("user_uid={$_SESSION['userid']}")->get();
//		sp($addressData);
		View::with('addressData',$addressData);
		View::make();
	}
	
	//添加新地址
	public function addAddress(){
		$realName = Q('post.realName');
		$area1 = Q('post.area1');
		$area2 = Q('post.area2');
		$area3 = Q('post.area3');
		$address = Q('post.address');
		$phone_num = Q('post.phone_num');
		if($realName == ''){
			echo json_encode(1);
			exit;
		}
		if($address == ''){
			echo json_encode(2);
			exit;
		}
		if($phone_num == ''){
			echo json_encode(0);
			exit;
		}
		$addressData = array(
			'area1'=>$area1,
			'area2'=>$area2,
			'area3'=>$area3,
			'address'=>$address,
		);
		$addressData = implode(',', $addressData);
		//重组一个新地址数组
		$newAddress = array(
			'address'=>$addressData,
			'real_name'=>$realName,
			'phone_num'=>$phone_num,
			'user_uid'=>$_SESSION['userid'],
		);
		if($newAddress){
			//添加到收货地址表
			$this->model->add($newAddress);
			echo json_encode(3);
		}
		
	}
	//删除收货地址
	public function delAddress(){
		$addid = Q('post.addid');
//		sp($addid);
		$this->model->where("addid={$addid}")->delete();
		echo json_encode(1);
	}
}




 ?>